/**
 * fileName: MR_YLES_Delete_file_Timed
 * @NApiVersion 2.1
 * @NScriptType MapReduceScript
 */
define(["require", "exports", "N/search", "N/record", "N/log", "N/file", "../tools/YLES_Tool", "../tools/YLES_SEARCH_TOOL"], function (require, exports, search, record, log, file, YLES_Tool_1, YLES_SEARCH_TOOL_1) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    exports.summarize = exports.reduce = exports.getInputData = void 0;
    function getInputData(cxt) {
        try {
            let days = (0, YLES_Tool_1.getScriptParams)('custscript_Save_Import_CSV_Day');
            let now = new Date();
            let arr = [];
            for (let i = 0; i < days; i++) {
                let date = new Date(now.getTime() - i * 24 * 60 * 60 * 1000);
                let year = date.getFullYear();
                let mouth = date.getMonth() + 1;
                let day = date.getDate();
                let name = "ibCsv_" + year + "-" + mouth + "-" + day;
                log.debug("name", name);
                arr.push(name);
            }
            let foldid = (0, YLES_SEARCH_TOOL_1.getCsvParentFolder)();
            //搜索yles_csv_fold下的文件夹
            let filters = [["parent", "anyof", foldid]];
            let fiter = [];
            if (arr.length > 0) {
                // @ts-ignore
                filters.push("AND");
                for (let l = 0; l < arr.length; l++) {
                    if (l != 0) {
                        fiter.push("AND");
                    }
                    fiter.push(["name", "isnot", arr[l]]);
                }
            }
            if (fiter.length > 0) {
                filters.push(fiter);
            }
            let columns = [search.createColumn({ name: "internalid", label: "内部 ID" }),
                search.createColumn({ name: "name", sort: search.Sort.ASC, label: "名称" })];
            let data = []; //存放要删除的文件夹的内部id
            let folderSearchObj = search.create({
                type: "folder",
                filters: filters,
                columns: columns
            });
            let rec = folderSearchObj.run().getRange({ start: 0, end: 1000 });
            if (rec.length > 0) {
                for (let li = 0; li < rec.length; li++) {
                    data.push(rec[li].getValue("internalid"));
                }
            }
            return data;
        }
        catch (e) {
            log.debug("get.e", e);
        }
    }
    exports.getInputData = getInputData;
    function reduce(context) {
        try {
            // @ts-ignore
            let value = JSON.parse(context.values);
            log.debug("value", value);
            let folderSearchObj = search.create({
                type: "folder",
                filters: [
                    ["internalid", "anyof", value]
                ],
                columns: [
                    search.createColumn({ name: "name", join: "file", label: "名称" }),
                    search.createColumn({ name: "internalid", join: "file", label: "内部 ID" }),
                    search.createColumn({ name: "name", sort: search.Sort.ASC, label: "名称" })
                ]
            });
            let rec = folderSearchObj.run().getRange({ start: 0, end: 1000 });
            if (rec.length > 0) {
                for (let i = 0; i < rec.length; i++) {
                    let fileid = rec[i].getValue({ name: "internalid", join: "file" });
                    file.delete({ id: fileid });
                }
            }
            record.delete({ type: "folder", id: value });
        }
        catch (e) {
            log.debug("map.e", e);
        }
    }
    exports.reduce = reduce;
    function summarize(summary) {
        log.debug("执行完成", " ");
    }
    exports.summarize = summarize;
});
